package io.github.wjehovah.yyn.adapter;

/**
 * Created by wuyong on 15/8/4.
 */

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import io.github.wjehovah.yyn.App;
import io.github.wjehovah.yyn.R;
import io.github.wjehovah.yyn.bean.Duanzi;


/**
 * Created by wuyong on 15/8/4.
 */
public class DuanziAdapter extends BaseAdapter implements View.OnClickListener {
    private String id_love;
    private String id_hate;
    /**
     * 某一条id为id=15067760，点赞的url；
     */
    String REQUEST_JOKE_ZHAN=null;
    String REQUEST_JOKE_CAI=null;
    private Duanzi duanzi;
    private LayoutInflater mLayoutInflater;
    private Context context;
    public DuanziAdapter(Duanzi duanzi, Context context) {
        this.duanzi = duanzi;
        mLayoutInflater = LayoutInflater.from(context);
        this.context=context;
    }

    @Override
    public int getCount() {
        return duanzi.list.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mLayoutInflater.inflate(R.layout.duanzi_item, null, false);
            convertView.setTag(position);//
            holder.usericon = (ImageView) convertView.findViewById(R.id.iv_top_item_usericon);//用户头像
            holder.username = (TextView) convertView.findViewById(R.id.tv_top_item_username);//用户名
            holder.passtime = (TextView) convertView.findViewById(R.id.tv_top_item_passtime);//审核通过时间
            holder.content = (TextView) convertView.findViewById(R.id.tv_essenece_duanzi_content);//内容


            holder.love = (TextView) convertView.findViewById(R.id.tv_foot_love);//顶
            holder.hate = (TextView) convertView.findViewById(R.id.tv_foot_hate);//踩
            holder.transmit = (TextView) convertView.findViewById(R.id.tv_foot_transmit);//分享
            holder.comment = (TextView) convertView.findViewById(R.id.tv_foot_commnet);//评论
//            holder.love.setTag(position);
//            holder.hate.setTag(position);
//            holder.transmit.setTag(position);
//            holder.comment.setTag(position);
            //        设置监听
            holder.love.setOnClickListener(this);
            holder.hate.setOnClickListener(this);
            holder.transmit.setOnClickListener(this);//分享
            holder.comment.setOnClickListener(this);//评论
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.love.setTag(position);
        holder.hate.setTag(position);
        holder.transmit.setTag(position);
        holder.comment.setTag(position);

        Duanzi.Joke joke = duanzi.list.get(position);//获取单条的段子内容Item；
//            Bitmap bm = null;//获取profile_image，从网络获取bitmap；
//            holder.iv_user_icon.setImageBitmap(null);//用户名头像；

        BitmapUtils bitmapUtils = new BitmapUtils(App.getContext());
        bitmapUtils.display(holder.usericon, joke.profile_image);//用户名头像；

        holder.username.setText(joke.screen_name);//用户名
        holder.passtime.setText(joke.created_at);//时间
        holder.content.setText(joke.text);//内容
        holder.love.setText(joke.love);//顶
        holder.hate.setText(joke.hate);//踩
        holder.transmit.setText(joke.repost);//分享
        holder.comment.setText(joke.comment);//评论
        return convertView;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public void onClick(final View v) {
        HttpUtils httpUtils=new HttpUtils();
        final Duanzi.Joke joke = duanzi.list.get((int) v.getTag());
        switch (v.getId()) {
            case R.id.tv_foot_love:

                id_love=joke.id;
                Log.e("TAG","id_love=="+id_love);
                REQUEST_JOKE_ZHAN="http://api.budejie.com/api/api_open.php?market=wandoujia&udid=865528029212234&a=love&appname=baisibudejie&c=data&os=4.4.2&client=android&id="+id_love+"&tj_from=text&visiting=&mac=f4%3A9f%3Af3%3A24%3A66%3A53&ver=5.9.9&dong=love";
                httpUtils.send(HttpRequest.HttpMethod.GET, REQUEST_JOKE_ZHAN, new RequestCallBack<String>() {

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        //点赞成功，加一；
                        if (!responseInfo.result.equals("0")){
                            Log.e("ZZZ","点赞指向的URL=="+REQUEST_JOKE_ZHAN);
                            Log.e("ZZZ","点赞返回的数据"+responseInfo.result);
                            int loves=Integer.parseInt(joke.love) + 1;
                            ((TextView) v).setText(String.valueOf(loves));
                        }
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {

                    }
                });
                break;
            case R.id.tv_foot_hate:
                id_hate=joke.id;
                Log.e("TAG","id_hate=="+id_hate);
                REQUEST_JOKE_CAI="http://api.budejie.com/api/api_open.php?market=wandoujia&udid=865528029212234&a=cai&appname=baisibudejie&c=data&os=4.4.2&client=android&id="+id_hate+"+&tj_from=text&visiting=&mac=f4%3A9f%3Af3%3A24%3A66%3A53&ver=5.9.9&dong=cai";
                httpUtils.send(HttpRequest.HttpMethod.GET, REQUEST_JOKE_CAI, new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        //点赞成功，加一；
                        if (!responseInfo.result.equals("0")){
                            Log.e("CCCC","踩指向的URL=="+REQUEST_JOKE_CAI);
                            Log.e("CCCC","踩返回的数据"+responseInfo.result);
                            int hates=Integer.parseInt(joke.hate) + 1;
                            ((TextView) v).setText(String.valueOf(hates));
                        }
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {

                    }
                });
                break;
            case R.id.tv_foot_transmit:
                Toast.makeText(context, "tv_share", Toast.LENGTH_SHORT).show();
                break;
            case R.id.tv_foot_commnet:
                Toast.makeText(context, "tv_comment", Toast.LENGTH_SHORT).show();
        }
    }

    static class ViewHolder {

        ImageView usericon;
        TextView username;
        TextView passtime;
        TextView content;
        TextView love;
        TextView hate;
        TextView transmit;
        TextView comment;
    }
}


